package yxy.game.pm2.command.pickedtroops;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PickedTroopsMessage;
import yxy.game.pm2.module.pickedtroops.PickedTroops;
import yxy.game.pm2.module.pickedtroops.PickedTroopsHandle;
import yxy.game.pm2.module.pickedtroops.PickedTroopsModule;

/****************************************
 * @name CMD_PickedTroopsInfo
 * @description 兵符槽信息
 * @author 梁健彬
 * @since 2022-05-19
 ***************************************/
@MessageType(proto = Command.PickedTroopsInfo, classtype = PickedTroopsMessage.PickedTroopsInfoRequest.class)
public class CMD_PickedTroopsInfo extends Command<PickedTroopsMessage.PickedTroopsInfoRequest> {

    @Override
    public void execute(MessageSource messageSource, PickedTroopsMessage.PickedTroopsInfoRequest request) {
        String playerCode = messageSource.getTag();

        PickedTroopsMessage.PickedTroopsInfoResponse.Builder response = PickedTroopsMessage.PickedTroopsInfoResponse.newBuilder();

        try {
            PickedTroopsModule ptsmodul = PickedTroopsModule.get(playerCode);
            if (ptsmodul == null) {
                send(messageSource, response, Result.DataError);//不存在参数错误
                return;
            }

            for (PickedTroops pts : ptsmodul.getPts().values()) {
                response.addPts(PickedTroopsHandle.getPickedTroops(playerCode, pts));
            }

            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }


    private void send(MessageSource messageSource, PickedTroopsMessage.PickedTroopsInfoResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }

}